package com.arges.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.arges.web.common.Constant;
import com.arges.web.common.FileUtil;

/**
 * 文件上传控制层
 * 
 * @author zxj
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/upload")
public class UpLoadController
{
	@RequestMapping(value = "uploadFile.do", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
	@ResponseBody
	public String uploadFile(@RequestParam("pic") CommonsMultipartFile pic, HttpServletRequest request)
	{
		// 设置文件保存的本地路径
		String filePath = request.getSession().getServletContext().getRealPath("/uploadFiles/");
		String fileName = pic.getOriginalFilename();
		String fileType = fileName.split("[.]")[1];
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String uuidFileName = uuid + fileName;
		File f = new File(filePath + "/" + uuid + "." + fileType);

		// 将文件保存到服务器
		try
		{
			FileUtil.upFile(pic.getInputStream(), uuidFileName, filePath);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return Constant.ERROR;
		}
		return uuidFileName;
	}

	@RequestMapping("test.do")
	@ResponseBody
	public void test()
	{
		String result = "aaa";
	}
}
